<?php
/*
 +----------------------------------------------------------------------
 + Title        : AdminBase 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/6/25 10:19
 + Last-time    : 2019/6/25 10:19+ 86187
 + Desc         : AdminBase
 +----------------------------------------------------------------------
*/


namespace app\common\controller;


use library\Controller;

class AdminBase extends Controller
{
    /**
     * 返回失败的操作
     *
     * @param mixed   $info 消息内容
     * @param array   $data 返回数据
     * @param integer $code 返回代码
     */
    public function error($info, $data = [], $code = 1)
    {
        $result = [
            'code' => $code,
            'msg'  => $info,
            'data' => $data
        ];
        throw new \think\exception\HttpResponseException(json($result));
    }

    /**
     * 返回成功的操作
     *
     * @param mixed   $info 消息内容
     * @param array   $data 返回数据
     * @param integer $code 返回代码
     * @param string $url 返回代码
     */
    public function success($info, $data = [],$url='', $code = 0)
    {
        $result = [
            'code' => $code,
            'msg'  => $info,
            'data' => $data,
            'url'  => $url
        ];
        throw new \think\exception\HttpResponseException(json($result));
    }


    /**
     * 列表
     *
     * @auth   true
     * @menu   true
     * @return mixed
     * @author : Randy_chen
     * @Date   : 2019/7/13
     * @Time   : 10:57
     */
    public function index()
    {
        if ($this->request->post()) {
            $res = $this->_query($this->table)
                        ->order('id desc')
                        ->page(true, false, '', request()->param('limit'));
            $res['data'] = $res['list'];
            $this->success('查询成功!', $res);
        } else {
            $this->applyCsrfToken();
            return $this->fetch();
        }
    }

    /**
     *修改
     *
     * @auth   true
     * @author : Randy_chen
     * @Date   : 2019/7/13
     * @Time   : 10:58
     */
    public function edit()
    {
        $this->isAddMode = '0';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 添加.
     *
     * @auth true
     *
     * @return mixed
     */
    public function add()
    {
        $this->isAddMode = '1';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 删除.
     *
     * @auth true
     * @return mixed
     */
    public function del()
    {
        $this->_delete($this->table);
    }


    /**
     * 列表数据处理.
     *
     * @param array $data
     *
     * @throws \Exception
     */
    protected function _index_page_filter(&$data)
    {

    }

    /**
     * 删除结果处理.
     *
     * @param bool $result
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    protected function _del_delete_result($result)
    {
    }


    /**
     * 表单数据处理.
     *
     * @param array $data
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _form_filter(&$data)
    {

    }

    /**
     * 表单结果处理.
     *
     * @param bool $result
     */
    protected function _form_result($result)
    {

        if ($result && $this->request->isPost()) {
            $this->success('编辑成功！', 'javascript:history.back()');
        }else{
            $this->error('编辑失败！');
        }
    }

}